<?php
/**
 * Created by PhpStorm.
 * User: lenovo
 * Date: 2020/7/27
 * Time: 20:31
 */

namespace App\Services;


use App\Logic\UserGroupLogic;
use App\Model\GroupSysMsgModel;
use App\Model\UserGroupModel;

class UserGroupService
{
    /**设置群
     * @param $param
     * @param $userInfo
     */
    public function groupSetting($param,$userInfo){
        try{
            $userGroupId = UserGroupModel::create()->where(['group_id'=>$param['extra']['group_id'],'user_id'=>$userInfo['id']])->column('id');
            $sysmessage = 0;
            #判断是否在本群
            if(empty($userGroupId)){
                return ReponseCodeService::CODE_NOT_GROUP_MEMBER;
            }
            #修改状态
            $updateInfo = array();
            if(isset($param['extra']['msg_setting'])){
                $updateInfo['msg_setting'] = $param['extra']['msg_setting'];
            }
            if(isset($param['extra']['top'])){
                $updateInfo['top'] = $param['extra']['top'];
            }

            if(isset($param['extra']['nickname_in_group'])){
                $sysmessage = 1;
                $updateInfo['nickname_in_group'] = $param['extra']['nickname_in_group'];
            }
            if(isset($param['extra']['group_rename'])){
                $sysmessage = 1;
                $updateInfo['group_rename'] = $param['extra']['group_rename'];
            }

            if(empty($updateInfo)){
                return ReponseCodeService::CODE_EMPTY;
            }

            $updateInfo['version'] = HelpService::createVersion();
            $updateInfo['updated_at'] = HelpService::msectimeToStr(HelpService::msectime());

            if(UserGroupModel::create()->update( $updateInfo,['id'=>$userGroupId])){
                #生成系统消息
                if($sysmessage){
                    $userGroupLogic = new UserGroupLogic();
                    $userGroupWhere = [''];
                    $userGroup = $userGroupLogic->findGroupMember( $userGroupWhere );

                }

                return ReponseCodeService::CODE_OK;
            }
            return ReponseCodeService::CODE_FAILE;
        }catch(\Throwable $t){
            echo $t->getMessage();
            return ReponseCodeService::CODE_SERVICE_UNAVAILABLE;
        }
    }
}